/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import th.dao.KodJantinaDAO;
import th.dao.KodNegeriDAO;
import th.dao.KodTransaksiDAO;
import th.manager.ReportManager;
import th.manager.SMSgatewayManager;
import th.model.KodJantina;
import th.model.KodNegeri;
import th.model.KodTransaksi;
import th.model.Sms;
import th.model.Taha;
import th.stripes.BaseActionBean;
import th.stripes.util.ReportUtil;

/**
 *
 * @author fikri
 */
@UrlBinding("/report")
public class ReportAction extends BaseActionBean{
    
    private Taha taha;
    
    private List<Taha> senaraiTaha;
    
    private KodTransaksi kodTransaksi;
    
    private String tarikhHantar;
    
    private String noPhone;
    
    private String statusPenghantaran;
    
    private List<Map<String,String>> senaraiStatTaha;
    
    private List<Map<String,String>> senaraiStatUrusan;
    
    private List<Sms> senaraiSms;
    
    @Inject
    private ReportManager manager;
    
    @Inject
    private KodNegeriDAO kodNegeriDAO;
    
    @Inject
    private KodJantinaDAO kodJantinaDAO;
    
    @Inject
    private KodTransaksiDAO kodTransaksiDAO;
    
    @Inject
    private SMSgatewayManager smsService;
    
    @Inject 
    private ReportUtil reportUtil;
    
    private static Logger LOG = LoggerFactory.getLogger(ReportAction.class);
    
    private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    
    
    @DefaultHandler
    public Resolution showFormStatistikKelabTaha() {
        return new JSP("report/stat_form.jsp");
    }
    
    public Resolution statByNegeriForm() {
        return new JSP("report/stat_mengikut_negeri.jsp").addParameter("popup", true);
    }
    
    public Resolution statByJantinaForm() {
        return new JSP("report/stat_kelab_mengikut_jantina.jsp").addParameter("popup", true);
    }
    
    public Resolution showFormStatisticReport() {
        return new JSP("report/stat_penggunaan_mytaha_form.jsp").addParameter("popup", true);
    }
    
    public Resolution showFormSmsReport() {
        return new JSP("report/sms_report.jsp").addParameter("popup", true);
    }
    
     public Resolution searchByNegeri() {
        List<Object[]> senaraiObj = manager.searchKelabTahaStat(taha);
        senaraiStatTaha = new ArrayList<Map<String,String>>();
        for (Object[] obj : senaraiObj) {
             if (obj != null) {
                 Map<String,String> map = new HashMap<String,String>();
                 String count = obj[0].toString();
                 String kodNegeri = obj[1].toString();
                 String kodJantina = "";
                 String jantina = "";                 
                 String namaNegeri = "";
                 if (StringUtils.isNotBlank(kodNegeri)) {
                     KodNegeri negeri = kodNegeriDAO.findById(kodNegeri);
                     if (negeri != null) {
                         namaNegeri = negeri.getDesc();
                         map.put("count", count);
                         map.put("negeri", namaNegeri);
                         
                     }
                 }
                 if (obj.length > 2) {
                     if (obj[2] != null && !"".equals(obj[2].toString())) {
                     kodJantina = obj[2].toString();
                     KodJantina kj = kodJantinaDAO.findById(kodJantina);
                     if (kj != null) {
                         jantina = kj.getNama();
                         map.put("jantina", jantina);
                         
                     }
                 }
                 }   
                 if (!map.isEmpty())senaraiStatTaha.add(map);
             }
        }
         
//         senaraiTaha = manager.searchKelabTahaStat(taha);
         
        return new JSP("report/stat_mengikut_negeri.jsp").addParameter("popup", true);
    }
     
     public Resolution searchByJantina() {
        List<Object[]> senaraiObj = manager.searchTahaByJantina(taha);
        senaraiStatTaha = new ArrayList<Map<String,String>>();
        for (Object[] obj : senaraiObj) {
             if (obj != null) {
                 Map<String,String> map = new HashMap<String,String>();
                 String count = obj[0].toString();
                 String kodJantina = obj[1].toString();
                 String namaJantina = "";
                 if (StringUtils.isNotBlank(kodJantina)) {
                     KodJantina jantina = kodJantinaDAO.findById(kodJantina);
                     
                     if (jantina != null) {
                         namaJantina = jantina.getNama();
                         map.put("count", count);
                         map.put("jantina", namaJantina);
                         senaraiStatTaha.add(map);
                     }
                 }
             }
        }
        return new JSP("report/stat_kelab_mengikut_jantina.jsp").addParameter("popup", true);
    }
     
     public Resolution searchByUrusan() {
         if (kodTransaksi != null){
            List<Object[]> senaraiObj = manager.searchByUrusan(kodTransaksi);
            senaraiStatUrusan = new ArrayList<Map<String,String>>();
            for (Object[] obj : senaraiObj) {
                if (obj != null) {                    
                    Map<String,String> map = new HashMap<String,String>();
                    String count = obj[0].toString();
                    String month = obj[1].toString();
                    map.put("trans", kodTransaksiDAO.findById(kodTransaksi.getKod()).getNama());
                    map.put("count", count);
                    map.put("month", month);
                    senaraiStatUrusan.add(map);
                }
            }            
         } 
         return new JSP("report/stat_penggunaan_mytaha_form.jsp").addParameter("popup", true);
     }
     
     public Resolution searchSmsReport() {
         
         if (StringUtils.isNotBlank(tarikhHantar)) {             
             try {
                 Date reportDate = sdf.parse(tarikhHantar);
                 sdf.applyPattern("yyyy-MM-dd");
                 String th = sdf.format(reportDate);
                 senaraiSms = smsService.smsReport(th, noPhone, statusPenghantaran);
             } catch (Exception ex) {
                 LOG.error("ex {}", ex);
             }
         }         
        
         return new JSP("report/sms_report.jsp").addParameter("popup", true);
     }
     
     public Resolution cetakByNegeri() {
         
        List<Object[]> senaraiObj = manager.searchTahaByNegeri(taha);        
        senaraiStatTaha = new ArrayList<Map<String,String>>();
         Map<String, Object> param = new HashMap<String, Object>();
         int sum = 0;
         
         for (Object[] obj : senaraiObj) {
             if (obj != null) {
                 Map<String,String> map = new HashMap<String,String>();
                 String count = obj[0].toString();
                 String kodNegeri = obj[1].toString();
                 String namaNegeri = "";
                 if (StringUtils.isNotBlank(kodNegeri)) {
                     KodNegeri negeri = kodNegeriDAO.findById(kodNegeri);
                     if (negeri != null) {
                         namaNegeri = negeri.getDesc();
                         map.put("count", count);
                         map.put("negeri", namaNegeri);
                         sum = sum + Integer.parseInt(count);
                         senaraiStatTaha.add(map);
                     }
                 }
             }
        }
         
         param.put("senarai", senaraiStatTaha);
         param.put("date_now", sdf.format(new Date()));
         param.put("sum", sum);
        
         
         final byte[] b = reportUtil.generateReportIText( "tahaByNegeri.st", param );
         
         return new StreamingResolution("application/pdf") {

            @Override
            public void stream(HttpServletResponse response) throws Exception {
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
                response.setHeader("Content-disposition", "inline; filename=kelab_taha_by_negeri.pdf");
                ServletOutputStream out = response.getOutputStream();
                out.write(b, 0, b.length);
                out.flush();
                out.close();

            }
        };         
     }
     
     public Resolution cetakByUrusan() {
         
        Map<String, Object> param = new HashMap<String, Object>();
         if (kodTransaksi != null){
            List<Object[]> senaraiObj = manager.searchByUrusan(kodTransaksi);
            senaraiStatUrusan = new ArrayList<Map<String,String>>();
            for (Object[] obj : senaraiObj) {
                if (obj != null) {                    
                    Map<String,String> map = new HashMap<String,String>();
                    String count = obj[0].toString();
                    String month = obj[1].toString();
                    map.put("trans", kodTransaksiDAO.findById(kodTransaksi.getKod()).getNama());
                    map.put("count", count);
                    map.put("month", month);
                    senaraiStatUrusan.add(map);
                }
            }            
         } 
         
         param.put("senarai", senaraiStatUrusan);
         param.put("date_now", sdf.format(new Date()));       
        
         
         final byte[] b = reportUtil.generateReportIText( "tahaByUrusan.st", param );
         
         return new StreamingResolution("application/pdf") {

            @Override
            public void stream(HttpServletResponse response) throws Exception {
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
                response.setHeader("Content-disposition", "inline; filename=report_by_urusan.pdf");
                ServletOutputStream out = response.getOutputStream();
                out.write(b, 0, b.length);
                out.flush();
                out.close();

            }
        };         
     }
     
     
    public Taha getTaha() {
        return taha;
    }

    public void setTaha(Taha taha) {
        this.taha = taha;
    }

    public List<Taha> getSenaraiTaha() {
        return senaraiTaha;
    }

    public void setSenaraiTaha(List<Taha> senaraiTaha) {
        this.senaraiTaha = senaraiTaha;
    }

    public List<Map<String, String>> getSenaraiStatTaha() {
        return senaraiStatTaha;
    }

    public void setSenaraiStatTaha(List<Map<String, String>> senaraiStatTaha) {
        this.senaraiStatTaha = senaraiStatTaha;
    }

    public KodTransaksi getKodTransaksi() {
        return kodTransaksi;
    }

    public void setKodTransaksi(KodTransaksi kodTransaksi) {
        this.kodTransaksi = kodTransaksi;
    }

    public List<Map<String, String>> getSenaraiStatUrusan() {
        return senaraiStatUrusan;
    }

    public void setSenaraiStatUrusan(List<Map<String, String>> senaraiStatUrusan) {
        this.senaraiStatUrusan = senaraiStatUrusan;
    }

    public String getTarikhHantar() {
        return tarikhHantar;
    }

    public void setTarikhHantar(String tarikhHantar) {
        this.tarikhHantar = tarikhHantar;
    }

    public List<Sms> getSenaraiSms() {
        return senaraiSms;
    }

    public void setSenaraiSms(List<Sms> senaraiSms) {
        this.senaraiSms = senaraiSms;
    }

    public String getNoPhone() {
        return noPhone;
    }

    public void setNoPhone(String noPhone) {
        this.noPhone = noPhone;
    }

    public String getStatusPenghantaran() {
        return statusPenghantaran;
    }

    public void setStatusPenghantaran(String statusPenghantaran) {
        this.statusPenghantaran = statusPenghantaran;
    }

}
